﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /// <summary>
    /// The live zoom tool, clic and drag towards the top of the screen will zoom in, towards the bottom will zoom out
    /// </summary>
    public class TLiveZoom : BaseTool
    {
        #region Fields
        bool _MouseDown;
        Point _InitialMouseLocation;
        RectangleD _InitialObjectView;
        double Zoomfactor;
        Point2D64D _InitialMouseObjectCoordinates;//=inversetransform(_InitialMouseLocation)
        RectangleD _TempObjectView;
        Point2D64D _TranslationVector;//The translation vector so the point clic is the center of the zoom

        #endregion

        #region Constructor
        /// <summary>
        /// The zoom tool, clic and drag towards the top of the screen will zoom in,
        /// towards the bottom will zoom out
        /// </summary>
        /// <param name="Viewport">The viewport the tool with act on</param>
        public TLiveZoom(BaseViewport Viewport)
            : base(Viewport,"GeoLiveZoom")
        {
            _MouseDown = false;
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            _MouseDown = true;
            Viewport.DoubleBuffereEnabled = true;
            _InitialMouseLocation = MouseArgs.Location;
            _InitialObjectView = Viewport.CurrentObjectView;
            _InitialMouseObjectCoordinates = Viewport.Object2ImageTransformation.InverseTransform(_InitialMouseLocation);

        }

        /// <summary>
        /// The method called when the mouse button is up
        /// </summary>
        public override void MouseUp(MouseEventArgs MouseArgs)
        {
            _MouseDown = false;
            Viewport.DoubleBuffereEnabled = false;
            RefreshViewport();
        }

        /// <summary>
        /// The method called when the mouse is moved
        /// </summary>
        public override void MouseMove(MouseEventArgs MouseArgs)
        {
            if (_MouseDown)
            {
                Zoomfactor = Math.Pow(2, Math.Abs(_InitialMouseLocation.Y-MouseArgs.Location.Y) * Vars.Setup.Tools.TLiveZoom.ZoomFactor);

                if (MouseArgs.Y < _InitialMouseLocation.Y)
                {
                    Zoomfactor = 1 / Zoomfactor;
                }
               
                Viewport.CurrentObjectView = RectangleD.FromCenter(
                    _InitialObjectView.Center,
                    _InitialObjectView.Width * Zoomfactor,
                    _InitialObjectView.Height * Zoomfactor,
                    YAxisDirection.UpSide);

                _TranslationVector = _InitialMouseObjectCoordinates -Viewport.Object2ImageTransformation.InverseTransform(_InitialMouseLocation);
                
                _TempObjectView = Viewport.CurrentObjectView;
                _TempObjectView.X += _TranslationVector.X;
                _TempObjectView.Y += _TranslationVector.Y;
                Viewport.CurrentObjectView = _TempObjectView;
                
                RefreshViewport();
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click and drag up to zoom in, down to zoom out");
        } 
        #endregion
    }
}
